package gwt.immunology.client.view;

import static gwt.immunology.shared.GenericDtoKeys.BIRTHDAY;
import static gwt.immunology.shared.GenericDtoKeys.CITY;
import static gwt.immunology.shared.GenericDtoKeys.COUNTRY;
import static gwt.immunology.shared.GenericDtoKeys.CREATION_DATE;
import static gwt.immunology.shared.GenericDtoKeys.DISTRICT;
import static gwt.immunology.shared.GenericDtoKeys.FIRST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.FLAT;
import static gwt.immunology.shared.GenericDtoKeys.GENDER;
import static gwt.immunology.shared.GenericDtoKeys.HOUSE;
import static gwt.immunology.shared.GenericDtoKeys.LAST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.MIDDLE_NAME;
import static gwt.immunology.shared.GenericDtoKeys.OTHER_INFO;
import static gwt.immunology.shared.GenericDtoKeys.REGION;
import static gwt.immunology.shared.GenericDtoKeys.STREET;
import static gwt.immunology.shared.utils.ImmunologyUtils.DD_MM_YYYY;

import com.google.gwt.uibinder.client.UiFactory;
import gwt.immunology.client.components.CustomBox;
import gwt.immunology.client.components.DateBoxEx;
import gwt.immunology.client.components.DatePickerEx;
import gwt.immunology.client.i18n.ImmunologyI18N;
import gwt.immunology.shared.GenericDto;
import gwt.immunology.shared.enums.GenderEnum;
import gwt.immunology.shared.utils.ImmunologyUtils;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AddPatientViewImpl extends Composite implements AddPatientView {
    private Presenter presenter;
    private static AddPatientViewImplUiBinder uiBinder = GWT.create(AddPatientViewImplUiBinder.class);

    interface AddPatientViewImplUiBinder extends UiBinder<Widget, AddPatientViewImpl> {
    }

    ImmunologyI18N i18n = GWT.create(ImmunologyI18N.class);

    public AddPatientViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
        this.creationDate.setText(DateTimeFormat.getFormat(DD_MM_YYYY).format(new Date()));
        this.birthday.getDatePicker().setWidth("160px");
    }

    @UiField public CustomBox customBox;
    @UiField public TextBox patientLastName;
    @UiField public TextBox patientFirstName;
    @UiField public TextBox patientMiddleName;
    @UiField public RadioButton genderMale;
    @UiField public RadioButton genderFemale;
    @UiField public DateBoxEx birthday;
    @UiField public TextBox country;
    @UiField public TextBox region;
    @UiField public TextBox district;
    @UiField public TextBox city;
    @UiField public TextBox street;
    @UiField public TextBox house;
    @UiField public TextBox flat;
    @UiField public TextBox creationDate;
    @UiField public TextArea additionalInfo;
    @UiField public Button buttonRegistration;
    @UiField public Button buttonReset;

    @UiFactory
    DateBoxEx initDateBox() {
        return new DateBoxEx(new DatePickerEx());
    }

    @UiHandler("buttonReset")
    void resetButtonClick(ClickEvent e) {
        this.patientLastName.setText("");
        this.patientFirstName.setText("");
        this.patientMiddleName.setText("");
        this.genderMale.setValue(false);
        this.genderFemale.setValue(false);
        this.birthday.reset();
        this.country.setText("");
        this.region.setText("");
        this.district.setText("");
        this.city.setText("");
        this.street.setText("");
        this.house.setText("");
        this.flat.setText("");
        this.additionalInfo.setText("");
    }

    @UiHandler("buttonRegistration")
    void registrationButtonClick(ClickEvent e) {
        if (isRequiredFieldsEntered()) {
        	GenericDto patient = new GenericDto();
        	patient.put(LAST_NAME, this.patientLastName.getText());
        	patient.put(FIRST_NAME, this.patientFirstName.getText());
        	patient.put(MIDDLE_NAME, this.patientMiddleName.getText());
        	patient.put(BIRTHDAY, this.birthday.getValue());
        	patient.put(COUNTRY, this.country.getText());
        	patient.put(CITY, this.city.getText());
        	patient.put(STREET, this.street.getText());
        	patient.put(HOUSE, this.house.getText());
        	patient.put(CREATION_DATE, new Date());
        	
    		if(this.genderMale.getValue() == true){
    			patient.put(GENDER, GenderEnum.MALE.toString());
    		} else {
    			patient.put(GENDER, GenderEnum.FEMALE.toString());
    		}
    		
    		if(!this.region.getText().trim().isEmpty()){
    			patient.put(REGION, this.region.getText());
    		}
    		
    		if(!this.district.getText().trim().isEmpty()){
    			patient.put(DISTRICT, this.district.getText());
    		}
    		
    		if(!this.flat.getText().trim().isEmpty()){
    			patient.put(FLAT, new Integer(this.flat.getText()));
    		}
    		
    		if(!this.additionalInfo.getText().trim().isEmpty()){
    			patient.put(OTHER_INFO, this.additionalInfo.getText());
    		}
    		
    		presenter.registerNewPatient(patient);
        }
    }
    
    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    private boolean isRequiredFieldsEntered() {
        boolean result = true;

        if (!isDigitTextBoxCorrect(this.flat)) {
            Window.alert(i18n.digitalFieldRequiredMessage());
            result = false;
        }

        if (this.patientLastName.getText().trim().isEmpty() || 
            this.patientFirstName.getText().trim().isEmpty() || 
            this.patientMiddleName.getText().trim().isEmpty() || 
           (this.genderFemale.getValue() == this.genderMale.getValue()) || 
           (this.birthday.getValue() == null) || 
            this.country.getText().trim().isEmpty() || 
            this.city.getText().trim().isEmpty() || 
            this.street.getText().trim().isEmpty() || 
            this.house.getText().trim().isEmpty()) {
                Window.alert(i18n.requiredFieldsMessageAsterisks());
                result = false;
        }

        return result;
    }

    private boolean isDigitTextBoxCorrect(final TextBox textBox) {
        String text = textBox.getText();
        if (!text.isEmpty() && !ImmunologyUtils.isDigit(text)) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                @Override
                public void execute() {
                    textBox.setFocus(true);
                    textBox.setSelectionRange(0, textBox.getText().length());
                }
            });
            return false;
        }
        return true;
    }

}
